<?php

namespace app\admin\controller\service\info;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 项目配置管理
 *
 * @icon fa fa-circle-o
 */
class Projectconfig extends Backend
{

    /**
     * Projectconfig模型对象
     * @var \app\admin\model\service\info\Projectconfig
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\service\info\Projectconfig;

    }



    /**
     * 编辑
     *
     * @param $ids
     * @return string
     * @throws DbException
     * @throws \think\Exception
     */
    public function edit($ids = 1)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            \app\admin\model\service\info\Projectconfig::create(['service_mobile'=>'18200000000','cooperate_mobile'=>'18200000000','user_notice'=>'切换到北京、上海、广州、成都、郑州体验效果更好','skill_notice'=>'这是一个服务端小广播','shop_notice'=>'这是一个商户端小广播','refund_notice'=>'1、这是一个退款注意事项.','state'=>1,'cancel_minute'=>10,'comment_day'=>7]);
            $row = $this->model->get($ids);
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        //视图渲染
        if (false === $this->request->isPost()) {
            $purchaseList = Db::name("service_config_text")->select();
            $this->assign("purchaseList", $purchaseList);
            $this->view->assign('row', $row);
            // print_r($row);die;
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');

        //处理数据
        $params["purchase_info_ids"] = $params["purchase_info_ids"] ? implode(",", $params["purchase_info_ids"]) : "";
        $params["after_purchase_info_ids"] = $params["after_purchase_info_ids"] ? implode(",", $params["after_purchase_info_ids"]) : "";
        
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                $row->validateFailException()->validate($validate);
            }
            $result = $row->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }

    public function new()
    {
        $row = $this->model->get(1);
        if (!$row) {
            \app\admin\model\service\info\Projectconfig::create(['service_mobile'=>'18200000000','cooperate_mobile'=>'18200000000','user_notice'=>'切换到北京、上海、广州、成都、郑州体验效果更好','skill_notice'=>'这是一个服务端小广播','shop_notice'=>'这是一个商户端小广播','refund_notice'=>'1、这是一个退款注意事项.','state'=>1,'cancel_minute'=>10,'comment_day'=>7]);
            $row = $this->model->get(1);
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                $row->validateFailException()->validate($validate);
            }
            $result = $row->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException|\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }


}
